﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EmailClient.DAO;
using EmailClient.DTO;
using System.IO;

namespace EmailClient
{
    public partial class Setting : Form
    {
        public Setting()
        {
            InitializeComponent();
        }

        private void btn_Brown_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog_ChoosePath = new FolderBrowserDialog();
            folderBrowserDialog_ChoosePath.ShowDialog();
            txt_PathSaveEmail.Text = folderBrowserDialog_ChoosePath.SelectedPath;
        }

        private void Setting_Load(object sender, EventArgs e)
        {
            DataTable dt = ParametersDAO.GetAllParameters();
            txt_PathSaveEmail.Text = dt.Rows[0]["WheretoSaveEmail"].ToString();
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = ParametersDAO.GetAllParameters();
                string oldPath = dt.Rows[0]["WheretoSaveEmail"].ToString();
                string newPath = txt_PathSaveEmail.Text;
                ParametersDAO.UpdateParameters(newPath);
                MoveFolder(oldPath + @"\EmailDB", newPath + @"\EmailDB");
                MessageBox.Show("Change Successfully!");
            }
            catch
            {
                MessageBox.Show("Can't change!");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        static public void CopyFolder(string sourceFolder, string destFolder)
        {
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destFolder, name);
                File.Copy(file, dest);
            }
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);
                CopyFolder(folder, dest);
            }
        }
        static public void MoveFolder(string sourceFolder, string destFolder)
        {
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destFolder, name);
                File.Move(file, dest);
            }
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);
                MoveFolder(folder, dest);
            }
            Directory.Delete(sourceFolder);
        }
    }
}
